package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.asm.Type;
import com.example.*;
import com.example.demo.entify.Menu;
import com.example.demo.entify.Role;
import com.example.demo.entify.RoleMenu;
import com.example.demo.entify.User;
import com.example.demo.entify.UserRole;
import com.example.demo.result.Result;
import com.example.demo.result.ResultFactory;
import com.example.demo.service.MenuService;
import com.example.demo.service.UserRoleService;

@RestController
@CrossOrigin
@RequestMapping("/userrole")

public class UserRoleController {
	@Autowired
	private UserRoleService UserRoleService;

	@Autowired
	private MenuService menuService;


	@RequestMapping(value = "/findAllMenu", method = RequestMethod.POST)
	public Result findMenu(@RequestParam("username") String username) {
		List<Menu> list = null;
		User user = UserRoleService.findRoleByname(username);

		Integer roleID=user.getRoles().getId();
		
		if (user != null) {
			Menu menu = new Menu();
			menu.setParentId(0);
			menu.setRoleMenu(new RoleMenu(null, roleID, null));
			// 一级菜单
			list = menuService.findMenu(menu);
			for (Menu item : list) {
				
				menu.setParentId(item.getId());

				List<Menu> children = menuService.findMenu(menu);
				item.setChildren(children);

			}
		}
		return ResultFactory.buildSuccessFactory(list);

	}

}
